const UaParser = require('ua-parser-js');
const parser = new UaParser();

const isMobile = () => {
	return parser.getDevice().type === 'mobile';
};

const isSafari = () => {
	return parser.getBrowser().name.includes('Safari');
};

const isAndroidWechat = () => {
	return parser.getBrowser().name.includes('WeChat') && parser.getOS().name.includes('Android');
};

const documentCopy = text => {
	// text area method
	const textArea = document.createElement('textarea');
	textArea.value = text;
	// make the textarea out of viewport
	textArea.style.position = 'fixed';
	textArea.style.left = '-999999px';
	textArea.style.top = '-999999px';
	textArea.setAttribute('readonly', '');
	document.body.appendChild(textArea);
	textArea.focus();
	textArea.select();
	if (document.execCommand('copy')) {
		this.$Toast.success('链接已复制');
	}
	textArea.remove();
};

const copyToClipboard = text => {
	if (navigator.clipboard && navigator.clipboard.writeText) {
		navigator.clipboard.writeText(text).then(
			() => {
				/* clipboard successfully set */
			},
			err => {
				/* clipboard write failed */
				console.error('clipboard write failed : ', err);
				documentCopy(text);
			}
		);
	} else {
		documentCopy(text);
	}
};

// 更新聊天区高度
const updateMessageBoxHeight4Mobile = (type) => {
	const container = document.getElementById('zco-im');
	let title = document.getElementById('mobile-title');
	if (type === 'web') {
		title = null;
	}
	const messageBox = document.getElementById('chat-message');
	const footer = document.getElementById('mobile-footer');
	if (container && title && messageBox && footer) {
		if (container.clientHeight) {
			messageBox.style.height = container.clientHeight - title.clientHeight - footer.clientHeight + 'px';
		} else {
			messageBox.style.height = document.documentElement.clientHeight - 50 - 50 + 'px';
		}

		console.log('messageBox:', container.clientHeight, messageBox.style.height, footer.clientHeight);
	} else if (container && messageBox && footer) {
		if (container.clientHeight) {
			messageBox.style.height = container.clientHeight - footer.clientHeight + 'px';
		} else {
			messageBox.style.height = document.documentElement.clientHeight - 50 - 50 + 'px';
		}
		messageBox.style.maxHeight = messageBox.style.height;

		console.log('messageBox web:', container.clientHeight, messageBox.style.height, footer.clientHeight);
	}
};

const getAgentStatus = status => {
	let result = '';
	switch (status) {
		case 'canceled':
			result = '坐席取消邀请';
			break;
		case 'notfound':
			result = '无坐席在线';
			break;
		case 'timeout':
			result = '坐席呼叫超时';
			break;
		case 'offday':
			result = '坐席不在工作日';
			break;
		case 'offtime':
			result = '坐席不在工作时间';
			break;
		case 'scenariodisabled':
			result = '业务场景未启用';
			break;
		case 'otherCustomerAccept':
			result = '其他坐席已经接听';
			break;
		case 'multiCustomerForbidAI':
			result = '指定机器人呼叫时会话中存在多访客';
			break;
		case 'rejected':
			result = '坐席拒绝';
			break;
		case 'ringingTimeout':
			result = '振铃超时未接听';
			break;
		default:
			result = '';
			break;
	}
	return result;
};

export { isMobile, isSafari, isAndroidWechat, copyToClipboard, updateMessageBoxHeight4Mobile, getAgentStatus };
